<template>
  <div class="msg-item"
    v-bind:class="{'msg-left': left, 'msg-right': right}"
  >
    <img src="/static/background.jpg" :title="data.from.username">
    <div
      class="content"
      v-if="data.type === ''"
    >{{ data.data }}</div>
    <div
      class="content"
      v-else
    >
      <viewer :images="[data.data]" v-viewer="{movable: false}">
        <img :src="data.data" alt="">
      </viewer>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'Message',
  data () {
    return {
      left: false,
      right: false,
      type () {
        return this.data.from === this.userinfo ? 'msg-right' : 'msg-left'
      }
    }
  },
  computed: {
    ...mapState([
      'userinfo'
    ])
  },
  props: {
    data: Object
  },
  mounted () {
    this.left = this.data.from !== this.userinfo
    this.right = !this.left
  }
}
</script>

<style lang="stylus" scoped>
  .msg-item
    width 680px
    float left
    margin-bottom 15px
    >img
      width 40px
      height 40px
      border-radius 3px
      cursor pointer
    .content
      line-height 20px
      font-size 14px
      font-weight 400
      max-width 550px
      padding 10px 10px
      background-color #b1e281
      position relative
      word-wrap:break-word
      &:before
        content ''
        position absolute
        border 5px solid #b1e281
        top 15px
        transform rotate(45deg)
      img
        max-width 300px
        max-height 300px
  .msg-left
    >img
      margin-right 15px
      float left
    .content
      float left
      &:before
        left -5px
  .msg-right
    >img
      margin-left 15px
      float right
    .content
      float right
      &:before
        right -5px
</style>
